
import java.sql.*;
import java.util.Properties;

public class testDBF {

    public static void main(String[] args) throws Exception {
        try {
            String jdbURL = "jdbc:dbf://///home/ronaldo/.wine/dosdevices/d:/fersera en 192.168.5.3";
            //String jdbURL = "jdbc:dbf://///home/ronaldo/Escritorio";

            Class.forName("com.caigen.sql.dbf.DBFDriver");

            Properties props = new Properties();
            props.setProperty("delayedClose", "0");

            Connection conn = DriverManager.getConnection(jdbURL, props);
            Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

//            stmt.execute("CREATE TABLE X(D INT)");
//            //stmt.executeQuery();
//            
//            if (1 == 1) {
//                return;
//            }
            //           String sqll = ""
//                    + "SELECT a.agente, c.nombre, a.numero, a.cliente, "
//                    + " a.fecha, a.fecha + a.dias AS fchvence, a.saldo, "
//                    + " STR(YEAR(a.fecha + a.dias),4) + STRTRAN(STR(MONTH( a.fecha + a.dias), 2),\" \", \"0\") AS mes, "
//                    + " STR(YEAR(a.fecha + a.dias),4) + STRTRAN(STR(WEEKOFYEAR( a.fecha + a.dias), 2),\" \", \"0\") AS semana, "
//                    + " mesactual AS mesactual, weekactual AS semanaactual "
//                    + " FROM lananti a, agentes c "
//                    + " WHERE a.agente = c.codigo "
//                    + " AND a.saldo > 0 ";

            String sqll = "SELECT  lapli.nfactura AS numero, "
                    + " lapli.abonoa AS abono, "
                    + " lapli.fechaa AS fecha, "
                    + " RECNO() AS numreg,"
                    + " Month(Fecha) as mes, "
                    + " dolara AS dolar"
                    + " FROM lapli"
                    + " WHERE FECHA = '2004-07-31'"
                    + " ORDER BY numero ";
            ResultSet rs = stmt.executeQuery(sqll);
            ResultSetMetaData resultSetMetaData = rs.getMetaData();
            int nc = resultSetMetaData.getColumnCount();

            for (int i = 1; i <= nc; i++) {
                System.out.println(resultSetMetaData.getColumnName(i)
                        + "  " + resultSetMetaData.getColumnTypeName(i)
                        + "  " + resultSetMetaData.getPrecision(i)
                        + "  " + resultSetMetaData.getScale(i));
            }

            Object colval;

            while (rs.next()) {
                for (int j = 1; j <= nc; j++) {
                    colval = rs.getObject(j);
                    System.out.print(colval + "\t");
                }
                System.out.println();
            }

            rs.close();

            if (1 == 1) {
                return;
            }

            String sql = "create table if not exists testUsers  (FirstName varchar(25), LastName varchar(25));";
            System.out.println(sql);
            stmt.execute(sql);

            sql = "insert into testUsers values('John','Pull');";
            System.out.println(sql);
            stmt.execute(sql);

            sql = "insert into testUsers values('Tom','Cat');update testUsers set LastName='Tiger' where LastName='Cat';delete from testUsers where LastName='Tiger';";
            System.out.println(sql);
            stmt.execute(sql);

            sql = "SELECT FirstName+' '+LastName as UserName FROM testUsers";
            System.out.println(sql);
            rs = stmt.executeQuery(sql);
            resultSetMetaData = rs.getMetaData();
            int iNumCols = resultSetMetaData.getColumnCount();
            for (int j = 1; j <= iNumCols; j++) {
                System.out.println(resultSetMetaData.getColumnName(j)
                        + "  " + resultSetMetaData.getColumnTypeName(j)
                        + "  " + resultSetMetaData.getPrecision(j)
                        + "  " + resultSetMetaData.getScale(j));
            }


            while (rs.next()) {
                for (int j = 1; j <= iNumCols; j++) {
                    colval = rs.getObject(j);
                    System.out.print(colval + "\t");
                }
                System.out.println();
            }

            rs.close();

//                sql="drop table if exists testUsers;";
//                System.out.println(sql);
//                stmt.execute(sql);


            stmt.close();

            conn.close();
        } catch (SQLException sqle) {
            do {
                System.out.println(sqle.getMessage());
                System.out.println("Error Code:" + sqle.getErrorCode());
                System.out.println("SQL State:" + sqle.getSQLState());
                sqle.printStackTrace();
            } while ((sqle = sqle.getNextException()) != null);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}
